package com.soundShape.mediators.transactions
{
	
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.mediators.AbstractViewMediator;
	import com.soundShape.views.login.Login;
	import com.soundShape.views.transactions.Transactions;
	import com.soundShape.vo.CustomerVO;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.effects.Move;
	import mx.effects.Rotate;
	import mx.managers.PopUpManager;
	import mx.binding.utils.BindingUtils;
	import mx.events.ListEvent;
	import mx.controls.ComboBox;
	
	import spark.components.BorderContainer;
	
	
	
	
	public class TransactionsMediator extends AbstractViewMediator
	{
		
	
		
				
				public function TransactionsMediator()
				{
					super(Transactions);
				}
				
				/**
				 * Since the AbstractViewMediator sets the view via Autowiring in Swiz,
				 * we need to create a local getter to access the underlying, expected view
				 * class type.
				 */
				public function get view():Transactions
				{
					return this._view as Transactions;
				}
				
				/**
				 * The <code>init()</code> method is fired off automatically by the 
				 * AbstractViewMediator when the creation complete event fires for the
				 * corresponding ViewMediator's view. This allows us to listen for events
				 * and set data bindings on the view with the confidence that our view
				 * and all of it's child views have been created and live on the stage.
				 */
				override protected function init():void
				{
					super.init();
					
					this.setViews();
					
				}
				
				override protected function setViewDataBindings():void
				{
					super.setViewDataBindings();
					BindingUtils.bindProperty(this.view.cmbxCustomers,"dataProvider",this.applicationModel.customers,"source");
					setCustomerComboBox();
					
				}	
				
				override protected function setViewListeners():void
				{
					super.setViewListeners();
					
					//this.view.btnMenu.addEventListener(MouseEvent.CLICK, handleBtnMenu);
					
					this.view.cmbxCustomers.addEventListener(ListEvent.CHANGE, handleCmbxCustomersChange);
				}
				
				private function setViews():void
				{
					
				}
				
				
				/**
				 * *******************************************************************************
				 * Handlers
				 * *******************************************************************************
				 */
				private function handleBtnMenu(mouseEvent:MouseEvent):void
				{
					this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.SWITCH_TO_MENU_VIEW));
				}
				
				private function handleCmbxCustomersChange(listEvent:ListEvent):void
				{
					this.applicationModel.customerVO = ComboBox(listEvent.currentTarget).selectedItem as CustomerVO;
				}
				
				
				
				/**
				 * *******************************************************************************
				 * Swiz EventHanders
				 * *******************************************************************************
				 */
				
				/**
				 * *******************************************************************************
				 * util
				 * *******************************************************************************
				 */
				private function setCustomerComboBox():void
				{
					if(this.applicationModel.customerVO != null)
					{
						this.view.cmbxCustomers.selectedIndex = (this.view.cmbxCustomers.dataProvider as ArrayCollection).getItemIndex(this.applicationModel.customerVO);
					}
					else
					{
						this.view.cmbxCustomers.selectedIndex = -1;
					}
				}
				
			
		}
		
	}
